﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;
using System.IO;

namespace SIOsystem
{
    public partial class LoginForm : Form
    {
        public string uname = "";
        public long id = -1;
        public string rid = "";
        string dbpath = "";
        public LoginForm()
        {
            InitializeComponent();
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            if (LoginClass.login(userNameTextBox.Text, passwordTextBox.Text))
            {
                uname = userNameTextBox.Text;
                id = LoginClass.getOID(userNameTextBox.Text, passwordTextBox.Text);
                rid = LoginClass.getRID(id).ToString();
                this.DialogResult = DialogResult.OK; 
            }
            else
            {
                MessageBox.Show("用户名或密码错!");
            }
            ConfigHelper.UpdateConnectionStringsConfig("SIOsystem.Properties.Settings.DatabaseConnectionString"
    , "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbpath + ";Jet OLEDB:Database Password=;", false);
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            string connectionString=ConfigHelper.GetConnectionStringsConfig("SIOsystem.Properties.Settings.DatabaseConnectionString");
            int si=connectionString.IndexOf("ce=")+3;
            int ei=connectionString.IndexOf(";",si);
            if (ei>si)
            {
                dbpath=connectionString.Substring(si,ei-si);
            } 
            if (!File.Exists(dbpath))
            {
                MessageBox.Show("所配置数据库路径错误，自动恢复默认路径！");
                dbpath=Application.StartupPath + @"\database\Database.mdb";
                if (!File.Exists(dbpath))
                {
                    dbpath=Application.StartupPath + @"\database\Database.mdb";
                    MessageBox.Show("默认数据库路径不存在，请手工配置???.exe.config文件！");
                    Application.Exit();
                }
            }
            ConfigHelper.UpdateConnectionStringsConfig("SIOsystem.Properties.Settings.DatabaseConnectionString"
                ,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dbpath+";Jet OLEDB:Database Password=sywg1234;",true);
        }
    }
}
